/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-01-09 10:15:21
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-03-20 15:36:45
 * @FilePath: /yt-devops/lib/pages/devices/device_controll/device_controll_view.dart
 * @Description: 设备控制服务，包括参数设置和远程控制
 */

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/expanded_tab_view.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/model/device_obj/group_config_model.dart';
import 'package:yt_devops/model/device_obj/product_config_model.dart';
import 'package:yt_devops/model/device_obj/property_group_model.dart';
import 'package:yt_devops/model/device_obj/service_config_model.dart';
import 'package:yt_devops/model/device_obj/tab_item_config_model.dart';
import 'package:yt_devops/pages/devices/device_controll/component/util.dart';
import 'package:yt_devops/pages/devices/device_controll/page_component/charge_terminal/charge_termimal_list/charge_termimal_list_view.dart';
import 'package:yt_devops/pages/devices/device_controll/device_property_group_render/device_property_group_render.dart';
import 'package:yt_devops/pages/devices/device_controll/page_component/charge_terminal/charge_termimal_list/charge_termimal_list_view.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/site_mains_elec_detail/keep_aliver_wrapper.dart';
import 'package:yt_devops/pages/site/site_observer_list/views/my_top_tabbar.dart';
import 'package:yt_devops/utils/func_utils.dart';
import 'device_controll_controller.dart';

import 'package:bruno/bruno.dart';

import 'device_controll_utils.dart';
import 'device_service_render/device_service_render_widget.dart';

class DeviceControllPage extends GetView<DeviceControllController> {
  DeviceControllPage({Key? key}) : super(key: key);

  ServiceConfigModel? curSetviceConfigModel;

  final GlobalKey tabKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Obx(
      () =>
          getPageComponent() ??
          Scaffold(
            appBar: AppBarWidget.defaultAppBar(controller.title),
            body: ListView.builder(
              itemBuilder: (context, index) =>
                  getWidget(controller.groupConfigList[index]),
              itemCount: controller.groupConfigList.length,
            ),
          ),
    );
  }

  Widget getWidget(ProductConfigModel model) {
    switch (model.type) {
      case DeviceConfigType.group:
        return groupView(model);

      case DeviceConfigType.tab:
        return tabView(model as ProductConfigModel<TabItemConfigModel>);
      case DeviceConfigType.service:
        return GetBuilder<DeviceControllController>(
          builder: (controller) => DeviceServiceRenderWidget(
            deviceId: controller.deviceId,
            serviceConfigModel: model as ServiceConfigModel,
            connectStatus: controller.connectStatus,
            dataSource: controller.dataSource,
            setHandler: (service) {
              curSetviceConfigModel = service;
              controller.update();
            },
          ),
        );
      case DeviceConfigType.propertyGroup:
        return GetBuilder<DeviceControllController>(
          builder: (controller) => DevicePropertyGroupRender(
            connectStatus: controller.connectStatus,
            dataSource: controller.dataSource,
            propertyGroupModel: model as PropertyGroupModel,
          ),
        );

      case DeviceConfigType.component:
        return DeviceControllModuleUtil.getCustomComponent(
          name: model.id,
          deviceId: controller.deviceId,
          connectStatus: controller.connectStatus,
          passEditAuth: controller.hasEditAuthority(model.authority),
        );

      default:
        return Container();
    }
  }

  Widget groupView(ProductConfigModel model) {
    if (FuncUtils.isListEmpty(model.children)) {
      return Gaps.empty;
    } else {
      return Column(
        children: [
          Offstage(
            offstage: !controller.isShowGroupName(model as GroupConfigModel),
            child: DeviceControllUtils.createHead(model.name ?? '',
                tip: model.tip, isShowSetButton: false),
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) => getWidget(model.children![index]),
            itemCount: model.children!.length,
          ),
        ],
      );
    }
  }

  Widget tabView(ProductConfigModel<TabItemConfigModel> model) {
    List<TabItemConfigModel> chilren = model.children ?? [];
    List<String> tabs = chilren.map((e) => e.name ?? '').toList();
    var tabcontroller = controller.getTabController(tabs.length);
    return Column(
      children: [
        Container(
          width: double.infinity,
          color: Colors.white,
          alignment: Alignment.center,
          padding: EdgeInsets.symmetric(horizontal: 16.w),
          child: MyTopTabBar(
            tabcontroller,
            tabs,
          ),
        ),
        // BrnTabBar(
        //   tabs: tabs,
        //   controller: tabcontroller,
        //   isScroll: true,
        //   padding: EdgeInsets.symmetric(horizontal: 16.w),
        //   labelPadding: EdgeInsets.only(right: 8.w),
        // ),

        ExtendedTabBarView(
          controller: tabcontroller,
          children: chilren.map(
            (e) {
              if (FuncUtils.isListEmpty(e.children)) {
                return Gaps.empty;
              } else {
                return GetBuilder<DeviceControllController>(
                  builder: (controller) => KeepAliveWrapper(
                    ListView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemBuilder: (context, index) =>
                          getWidget(e.children![index]),
                      itemCount: e.children!.length,
                    ),
                  ),
                );
              }
            },
          ).toList(),
        ),
      ],
    );
  }

  Widget? getPageComponent() {
    if (controller.pageComponentMap.isNotEmpty) {
      Widget? widget;
      switch (controller.pageComponentMap!['id']) {
        case 'EVChargerTerminalConfig':
          widget = ChargeTermimalListPage(controller.deviceId);
        default:
          widget = null;
      }
      return widget;
    } else {
      return null;
    }
  }
}
